//===================================================================================
// $$
//
// Coder: Code Milker v1.0
// 0 - EntityName
// 1 - lower
//===================================================================================


namespace App.Data.SqlServer.Repositories {
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class TrainingRepository : BaseRepository, ITrainingRepository {
        public TrainingRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Training training) {
            this.GetDbSet<Training>().Add(training);
            this.UnitOfWork.SaveChanges();
        }

        public Training GetTraining(Guid trainingId) {
            return this.GetDbSet<Training>()
                .Where(c => c.Id == trainingId)
                .Single();
        }

        public void Update(Training updatedTraining) {
            this.GetDbSet<Training>().Attach(updatedTraining);
            this.SetEntityState(updatedTraining, updatedTraining.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid trainingId) {
            Training TrainingToDelete = this.GetTraining(trainingId);
            this.GetDbSet<Training>().Remove(TrainingToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<Training> Find()
        {
            return this.GetDbSet<Training>();
        }

        public IQueryable<Training> GetAll()
        {
            return this.GetDbSet<Training>();
        }
    }
}
